<?php

namespace app\controller\system;

use app\BaseController;
use think\response\Json;

class Role extends BaseController
{
    public function save(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Role::class)->insertData();
        });
    }

    public function lst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Role::class)->lst();
        });
    }

    public function query(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Role::class)->deatil();
        });
    }

    public function delete(): Json
    {
        return $this->handle(function () {
            $param = request()->param();
            $count = $this->app->make(\app\model\Account::class)->where('rid', $param['id'])->where('is_del', 1)->count();
            if ($count > 0) throw new \Exception('该权限下存在销售员，请先删除销售员！', 7001);
            $this->app->make(\app\model\Role::class)->where('id', $param['id'])->update(['is_del' => 0]);
            return ['errMsg' => '删除成功！'];
        });
    }

    public function search(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Role::class)->where('state', 1)->where('is_del', 1)->column("id,name");
        });
    }
}
